package com.service.admin.filecoin;

import com.service.admin.filecoin.exception.*;
import com.service.admin.filecoin.model.EasySend;
import com.service.admin.filecoin.model.GetGas;
import com.service.admin.filecoin.model.result.BalanceResult;
import com.service.admin.filecoin.model.result.GasResult;
import com.service.admin.filecoin.model.result.SendResult;
import com.service.admin.filecoin.model.result.WalletResult;
import com.service.admin.framework.log.RequestLog;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.BaseResponse;
import com.service.utility.common.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;


@Slf4j
@RestController
@Api(tags = "FileCoin API 相关")
@RequestMapping("open/file-coin")
public class FileCoinController extends BaseController {
    @Autowired
    private Filecoin filecoin;


    @PostMapping("/create")
    @ApiOperation("创建钱包地址")
    @RequestLog("创建钱包地址")
    public BaseResponse createWallet(HttpServletRequest request){
	    String uuid = UUIDUtil.randomUUID();
	    log.info("请求开始 Msg: 请求参数：{}，UUID:{}", request, uuid);
	    WalletResult wallet = null;
	    try {
		    wallet = filecoin.createWallet();
		    System.out.println(wallet);
	    } catch (WalletException e) {
		    e.printStackTrace();
	    }
		return successQueryOneResponse(wallet);
    }

	@PostMapping("/balanceOf")
	@ApiOperation("查询FIL余额")
	@RequestLog("查询FIL余额")
	public BaseResponse balanceOf(HttpServletRequest request, @RequestParam String address){
		String uuid = UUIDUtil.randomUUID();
		log.info("请求开始 Msg: 请求参数：{}，UUID:{}", request, uuid);
		BalanceResult balance = null;
		try {
			balance = filecoin.balanceOf(address);
		} catch (BalanceOfException e) {
			e.printStackTrace();
		} catch (ExecuteException e) {
			e.printStackTrace();
		}
		return successQueryOneResponse(balance);
	}

	@PostMapping("/send")
	@ApiOperation("FIL转账")
	@RequestLog("FIL转账")
	public BaseResponse send(HttpServletRequest request, @RequestBody EasySend send){
		String uuid = UUIDUtil.randomUUID();
		log.info("请求开始 Msg: 请求参数：{}，UUID:{}", request, uuid);
		SendResult sendResult = null;
		try {
			sendResult = filecoin.easySend(send);
		} catch (ParameException | SendException | ExecuteException e) {
			e.printStackTrace();
		}
		return successQueryOneResponse(sendResult);
	}

	@PostMapping("/nonce")
	@ApiOperation("nonce查询")
	@RequestLog("nonce查询")
	public BaseResponse getNonce(HttpServletRequest request, @RequestParam String address){
		String uuid = UUIDUtil.randomUUID();
		log.info("请求开始 Msg: 请求参数：{}，UUID:{}", request, uuid);
		int nonce = 0;
		try {
			nonce = filecoin.getNonce(address);
		} catch (ExecuteException | ParameException e) {
			e.printStackTrace();
		}
		return successQueryOneResponse(nonce);
	}

	@PostMapping("/gas")
	@ApiOperation("gas查询")
	@RequestLog("gas查询")
	public BaseResponse getGas(HttpServletRequest request, @RequestBody GetGas gas){
		String uuid = UUIDUtil.randomUUID();
		log.info("请求开始 Msg: 请求参数：{}，UUID:{}", request, uuid);
		GasResult gasResult = null;
		try {
			gasResult = filecoin.getGas(gas);
		} catch (ParameException | ExecuteException e) {
			e.printStackTrace();
		}
		return successQueryOneResponse(gasResult);
	}

}
